/*
 *  这里就优化一下二维数组的，换成一维数组
 * */

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {

    // 那这边为了方便测试，就不整输入输出了
    int n = 4, m = 8;
    int w[4] = {2, 3, 4, 5};
    int v[4] = {3, 4, 5, 6};

    vector<int> dp(m + 1, 0);

    for (int i = 1; i <= n; ++i) {
        for (int j = m; j >= w[i]; --j) {
            dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
        }
    }

    cout << dp[m] << endl;

    return 0;
}